package com.zackeus.sipservice.entity;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zackeus.common.utils.StringUtils;
import com.zackeus.sipservice.constant.Event;

/**
 * @name: SipServiceEvent
 * @author: zz
 * @date: 2022/9/19
 */
public class SipServiceEvent implements Parcelable {

    @Event.Unit
    final String tag;

    final String msg;

    public SipServiceEvent(@NonNull @Event.Unit String tag) {
        this(tag, null);
    }

    public SipServiceEvent(@Event.Unit String tag, @Nullable String msg) {
        this.tag = tag;
        this.msg = msg;
    }

    protected SipServiceEvent(Parcel in) {
        this.tag = in.readString();
        this.msg = in.readString();
    }

    public static final Creator<SipServiceEvent> CREATOR = new Creator<>() {
        @Override
        public SipServiceEvent createFromParcel(Parcel in) {
            return new SipServiceEvent(in);
        }

        @Override
        public SipServiceEvent[] newArray(int size) {
            return new SipServiceEvent[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(this.tag);
        parcel.writeString(this.msg);
    }

    public String getTag() {
        return tag;
    }

    @Nullable
    public JsonObject getMsg() {
        if (StringUtils.isBlank(this.msg))
            return null;
        return JsonParser.parseString(this.msg).getAsJsonObject();
    }
}
